<template>
  <div>
    <nav-bar></nav-bar>
    <form action="/">
      <van-search
        v-model.trim="value"
        show-action
        placeholder="请输入搜索关键词"
        @input="onSearch"
        @cancel="onCancel"
      />
    </form>
    <!-- 历史记录框 -->
    <div class="searchGoods" v-if="isShow == '0'">
      <div class="history">
        <div class="top">
          历史记录
          <div class="icon" @click="clear"></div>
        </div>

        <div class="cont">
          <div
            v-for="(item, index) in historyData"
            :key="index"
            @click="goodsInfo(item.keyword)"
          >
            {{ item.keyword }}
          </div>
        </div>
      </div>
      <div class="history">
        <div class="top">热门搜索</div>
        <div class="cont">
          <div
            v-for="(item, index) in hotKeywordList"
            :key="index"
            :class="index < 1 ? 'active' : ''"
            @click="goodsInfo(item.keyword)"
          >
            {{ item.keyword }}
          </div>
        </div>
      </div>
    </div>
    <!-- 搜索列表框 -->
    <div class="searchList" v-if="isShow == '1'">
      <div v-for="item in searchList" :key="item.id" @click="addList(item)">
        {{ item.name }}
      </div>
    </div>
    <!-- 商品搜索框 -->
    <div class="list" v-if="isShow == '2'">
      <div class="nav">
        <div
          v-for="(item, index) in chooseList"
          :key="index"
          @click="choose(index)"
          :class="{
            active: index == current,
            desc: index === 1 && flag,
            asc: index === 1 && flag == false,
            price: index === 1,
          }"
        >
          {{ item }}
        </div>
      </div>
      <div class="goodList">
        <div
          class="item"
          v-for="item in searchList"
          :key="item.id"
          @click="goGoods(item.id)"
        >
          <img :src="item.list_pic_url" alt="" />
          <p class="name">{{ item.name }}</p>
          <p class="price">￥{{ item.retail_price }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Navbar from "../../../components/Navbar";
import { indexaction } from "@/api/home/search";
import { helperaction } from "@/api/home/search/keyword.js";
import { addhistoryaction } from "@/api/home/search/addSearch.js";
import { clearhistoryAction } from "@/api/home/search/clearHistory.js";
import { Form } from "vant";
export default {
  props: {},
  data() {
    return {
      chooseList: ["综合", "价格", "分类"],
      value: "",
      hotKeywordList: [],
      historyData: [],
      isShow: "0", //0是历史记录框 1是搜索列表 2是商品列表
      searchList: [],
      goodInfoList: [],
      showList: [],
      current: 0,
      flag: true,
    };
  },
  computed: {},
  created() {
    this.init();
  },
  mounted() {},
  watch: {},
  methods: {
    init() {
      indexaction({
        openId: localStorage.getItem("openId"),
      }).then((res) => {
        // console.log(res);
        this.historyData = res.historyData;
        this.hotKeywordList = res.hotKeywordList;
      });
    },
    onSearch() {
      helperaction({
        keyword: this.value,
        order: "asc",
      }).then((res) => {
        console.log(res);
        this.searchList = res.keywords;
      });
      if (this.value.length > 0) {
        this.isShow = "1";
      } else {
        this.isShow = "0";
      }
    },
    onCancel() {},
    addList(el) {
      this.isShow = "2";
      this.value = el.name;
      // console.log(el)
      // this.showList=el
      addhistoryaction({
        keyword: el.name,
        openId: localStorage.getItem("openId"),
      }).then((res) => {
        console.log(res);
      });
      helperaction({
        keyword: el.name,
        order: "asc",
      }).then((res) => {
        //  console.log(res);
        this.searchList = res.keywords;
      });
    },
    goodsInfo(el) {
      this.isShow = "2";
      this.value = el;
      helperaction({
        keyword: el,
        order: "asc",
      }).then((res) => {
        //  console.log(res);
        this.searchList = res.keywords;
      });
    },
    clear() {
      clearhistoryAction({
        openId: localStorage.getItem("openId"),
      }).then((res) => {
        // console.log(res);
      });
      this.init();
    },
    choose(index) {
      this.current = index;
      if (index == 1) {
        this.flag = !this.flag;
      }
    },
    goGoods(id) {
      this.$router.push({ path: "/home/goods", query: { ids: id } });
    },
  },
  components: {
    "nav-bar": Navbar,
  },
};
</script>

<style lang="scss" scoped>
.history {
  background: #fff;
  padding: 16px;
  .top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
  }
  .icon {
    width: 27px;
    height: 27px;
    background: url(http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/del1-93f0a4add4.png)
      no-repeat;
    background-size: 100% 100%;
  }
  .cont {
    display: flex;
    flex-wrap: wrap;
    div {
      padding: 5px 5px;
      border: 0.5px solid #999;
      margin: 0 15px 10px 0;
    }
    .active {
      border: 1px solid #b4282d;
      color: #b4282d;
    }
  }
}
.searchList {
  position: absolute;
  height: 100%;
  width: 100%;
  top: 45px;
  left: 0;
  bottom: 0;
  box-sizing: border-box;
  padding: 0 16px;
  z-index: 9;
  background: #fff;
  overflow-y: scroll;
  margin-top: 50px;
  div {
    text-align: left;
    padding: 10px 0;
  }
}
.nav {
  display: flex;
  background: #fff;
  width: 100%;
  height: 40px;
  line-height: 40px;
  div {
    width: 125px;
    height: 100%;
    text-align: center;
  }
  .active {
    color: #b4282d;
  }
}
.goodList {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  .item {
    width: 186px;
    margin-bottom: 2.5px;
    text-align: center;
    background: #fff;
    padding: 7.5px 0;
    img {
      display: block;
      width: 151px;
      height: 151px;
      margin: 0 auto;
    }
    .name {
      margin: 7px 0 11px 0;
      text-align: center;
      padding: 0 10px;
      font-size: 12px;
    }
    .price {
      text-align: center;
      font-size: 15px;
      color: #b4282d;
    }
  }
}
.active.desc {
  background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/down-95e035f3e5.png)
    2.2rem no-repeat;
  background-size: 0.2rem 0.28rem;
}
.active.asc {
  background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/up-636b92c0a5.png)
    2.2rem no-repeat;
  background-size: 0.2rem 0.28rem;
}
.price {
  background: url(//yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/no-3127092a69.png)
    2.2rem no-repeat;
  background-size: 0.2rem 0.28rem;
}
</style>
